package testing;

import lights.adapters.Field;
import lights.adapters.Tuple;
import lights.interfaces.ITuple;
import lime2.Location;
import sagroup.dracots.policy.RPTuple;
import sagroup.dracots.policy.ReplicationPolicy;
import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.FlatShareTuple;
import sagroup.dracots.TupleType;

public class TestRTUPAgentC implements Runnable{

	
	DracoTS dts = null;

    private int port = 30000;
	  
	 
	  public void run()
	  {
		  try
		  {
			  dts = DracoTS.get("test");
			  ITuple it = new Tuple();
			  it.add(new Field().setValue("Test"));
			  FlatShareTuple at = new FlatShareTuple(new TupleType("A"), it);
			  FlatShareTuple bt = new FlatShareTuple(new TupleType("B"),it);
			  FlatShareTuple ct = new FlatShareTuple(new TupleType("C"),it);
			 // dts.out(at);
			 // dts.out(bt);
			//  dts.out(ct);
			 
			  Location fakeRemote = Location.build("localhost", port);
			  FlatShareTuple templateA  = FlatShareTuple.createTemplate(new TupleType("A"), null, false);
			  
			  FlatShareTuple templateB  = FlatShareTuple.createTemplate(new TupleType("B"), null, false);
			  templateB.setCurr(fakeRemote);
			  templateB.setDest(fakeRemote);
			  templateB.setMatchAll(true);
			  
			  
			  FlatShareTuple templateC  = FlatShareTuple.createTemplate(new TupleType("C"), null, false);
			  templateC.setCurr(fakeRemote);
			  templateC.setDest(fakeRemote);
			  templateC.setMatchAll(true);
			  
			  RPTuple rptB = new RPTuple(new TupleType("B"), ReplicationPolicy.RC);
			  RPTuple rptC =  new RPTuple(new TupleType("C"), ReplicationPolicy.FR);
			  
			  //dts.out(rptB);
			 
			  Thread.sleep(10000L);
			   dts.out(rptC);
			  
			  for(int i=0; i <= 1000; i++)
			  {
				  DracoTuple ret = dts.rdp(templateB);
				 // DracoTuple[] rets = dts.rdg(templateC);

				  try
				  {
				   Thread.sleep(2000L);
				  }catch(InterruptedException ex)
				  {
					  continue;
				  }
			  }
			  
		  }catch(Exception ex)
		  {
			  System.out.print(ex.getMessage());
		  }
		  System.out.println("Test Agent A is done");
	  } 
}
